#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>

using namespace std;

bool init();
void kill();
bool loop();

// Pointers to our window and surfaces
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture* texture, *text;
TTF_Font* font;
string input;


int main(int argc, char* argv[])
{
	if (!init())
	{
		system("pause");
		return 1;
	}
	while (loop())
	{
		SDL_Delay(10);
	}
	kill();
	return 0;
}


bool loop()
{
	static const unsigned char* keys = SDL_GetKeyboardState(NULL);

	SDL_Event e;
	SDL_Rect dest;

	// Clear the window to white
	SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
	SDL_RenderClear(renderer);

	// Event loop
	while (SDL_PollEvent(&e) != 0)
	{
		switch (e.type)
		{
		case SDL_QUIT:
			return false;
		case SDL_TEXTINPUT:
			input += e.text.text;
			break;
		case SDL_KEYDOWN:
			if (e.key.keysym.sym == SDLK_BACKSPACE && input.size())
			{
				input.pop_back();
			}
			break;
		default:
			break;
		}
	}

	// Render texture;
	SDL_RenderCopy(renderer, texture, NULL, NULL);

	SDL_Color foreground = { 0, 0, 0 };

	if (input.size())
	{
		SDL_Surface* text_surf = TTF_RenderText_Solid(font, input.c_str(), foreground);
		text = SDL_CreateTextureFromSurface(renderer, text_surf);

		dest.x = 320 - (text_surf->w / 2.0f);
		dest.y = 240;
		dest.w = text_surf->w;
		dest.h = text_surf->h;
		SDL_RenderCopy(renderer, text, NULL, &dest);

		SDL_DestroyTexture(text);
		SDL_FreeSurface(text_surf);
	}

	// Update window
	SDL_RenderPresent(renderer);

	return true;
}

bool init()
{
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
	{
		cout << "Error initializing SDL: " << SDL_GetError() << endl;
		return false;
	}

	// Initialize SDL_image with PNG loading subsystem
	if (IMG_Init(IMG_INIT_PNG) < 0)
	{
		cout << "Error initializing SDL_image: " << IMG_GetError() << endl;
		return false;
	}

	// Initialize SDL_ttf
	if (TTF_Init() < 0)
	{
		cout << "Error initializing SDL_mixer: " << TTF_GetError() << endl;
		return false;
	}

	window = SDL_CreateWindow("Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
	if (!window)
	{
		cout << "Error creating window: " << SDL_GetError() << endl;
		return false;
	}

	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	if (!renderer)
	{
		cout << "Error creating renderer: " << SDL_GetError() << endl;
		return false;
	}

	// Load bitmap into surface
	SDL_Surface* buffer = IMG_Load("test2.png");
	if (!buffer)
	{
		cout << "Error loading image test.png: " << SDL_GetError() << endl;
		return false;
	}

	// Create texture
	texture = SDL_CreateTextureFromSurface(renderer, buffer);
	// Free surface as it's no longer needed
	SDL_FreeSurface(buffer);
	buffer = NULL;
	if (!texture)
	{
		cout << "Error creating texture: " << SDL_GetError() << endl;
		return false;
	}

	// Load Font
	font = TTF_OpenFont("font.ttf", 72);
	if (!font)
	{
		cout << "Error loading font: " << TTF_GetError() << endl;
		return false;
	}

	// Start sending SDL_TextInput events
	SDL_StartTextInput();

	return true;
}


void kill()
{
	SDL_StopTextInput();

	TTF_CloseFont(font);
	SDL_DestroyTexture(texture);
	texture = NULL;

	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	window = NULL;
	renderer = NULL;

	TTF_Quit();
	IMG_Quit();
	SDL_Quit();
}